package com.xizimall.rest.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.xizimall.mapper.TbItemDescMapper;
import com.xizimall.mapper.TbItemMapper;
import com.xizimall.mapper.TbItemParamItemMapper;
import com.xizimall.pojo.TbItem;
import com.xizimall.pojo.TbItemDesc;
import com.xizimall.pojo.TbItemParamItem;
import com.xizimall.pojo.TbItemParamItemExample;
import com.xizimall.pojo.TbItemParamItemExample.Criteria;
import com.xizimall.rest.dao.JedisClient;
import com.xizimall.rest.service.ItemService;
import com.xizimall.common.pojo.XizimallResult;
import com.xizimall.common.utils.JsonUtils;
/**
 * 商品基本信息查询服务
 * <p>Title: ItemServiceImpl</p>
 * <p>Description: </p>
 * <p>华南理工大学研究生</p> 
 * @author	李隆熹
 * @date	2018年4月25日下午3:09:38
 * @version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {
    
	@Autowired
	private TbItemMapper itemMapper;
	
	@Autowired
	private TbItemDescMapper itemDescMapper;
	
	@Autowired
	private TbItemParamItemMapper itemParamItemMapper; 
	
	@Value("${REDIS_ITEM_KEY}")
	private String REDIS_ITEM_KEY;
	
	@Value("${REDIS_ITEM_EXPIRE}")
	private Integer REDIS_ITEM_EXPIRE;
	
	@Autowired
	private JedisClient jedisClient;
	
	@Override
	public XizimallResult getItemBaseInfo(long itemId) {
		//从缓存中去商品信息,商品id对应的商品信息
		String json=jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":base");
		//判断是否有值
		if(!StringUtils.isBlank(json)){
          //把json转换成java对象
			TbItem item=JsonUtils.jsonToPojo(json, TbItem.class);
			return XizimallResult.ok(item);
		}
		//根据商品id查询商品信息
		TbItem item=itemMapper.selectByPrimaryKey(itemId);
		//把商品存入缓存
		try {
			jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":base", JsonUtils.objectToJson(item));
			//设置key的有效期
			jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":base",REDIS_ITEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//使用xizimallresult包装一下
		return XizimallResult.ok(item);
	}

	@Override
	public XizimallResult getItemDesc(long itemId) {
		
		//从缓存中去商品信息,商品id对应的商品信息
				String json=jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":desc");
				//判断是否有值
				if(!StringUtils.isBlank(json)){
		          //把json转换成java对象
					TbItemDesc itemDesc=JsonUtils.jsonToPojo(json, TbItemDesc.class);
					return XizimallResult.ok(itemDesc);
				}
				//根据商品id查询商品信息
				TbItemDesc itemDesc=itemDescMapper.selectByPrimaryKey(itemId);
				//把商品存入缓存
				try {
					jedisClient.set(REDIS_ITEM_KEY+":"+itemId+":desc", JsonUtils.objectToJson(itemDesc));
					//设置key的有效期
					jedisClient.expire(REDIS_ITEM_KEY+":"+itemId+":desc",REDIS_ITEM_EXPIRE);
				} catch (Exception e) {
					e.printStackTrace();
				}
				//使用xizimallresult包装一下
				return XizimallResult.ok(itemDesc);
	}

	@Override
	public XizimallResult getItemParam(long itemId) {
		//添加缓存
				try {
					//添加缓存逻辑
					//从缓存中取商品信息，商品id对应的信息
					String json = jedisClient.get(REDIS_ITEM_KEY + ":" + itemId + ":param");
					//判断是否有值
					if (!StringUtils.isBlank(json)) {
						//把json转换成java对象
						TbItemParamItem paramItem = JsonUtils.jsonToPojo(json, TbItemParamItem.class);
						return XizimallResult.ok(paramItem);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
		//设置查询条件
		TbItemParamItemExample example=new TbItemParamItemExample();
		Criteria criteria=example.createCriteria();
		criteria.andItemIdEqualTo(itemId);
		List<TbItemParamItem>list=itemParamItemMapper.selectByExampleWithBLOBs(example);
		if (list != null && list.size()>0) {
			TbItemParamItem paramItem = list.get(0);
			try {
				//把商品信息写入缓存
				jedisClient.set(REDIS_ITEM_KEY + ":" + itemId + ":param", JsonUtils.objectToJson(paramItem));
				//设置key的有效期
				jedisClient.expire(REDIS_ITEM_KEY + ":" + itemId + ":param", REDIS_ITEM_EXPIRE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return XizimallResult.ok(paramItem);
		}
		return XizimallResult.build(400, "无此商品规格");
	
	}

}
